package com.wmh.baseservice.admin.controller;

import com.wmh.baseservice.admin.pojo.SysAdvice;
import com.wmh.baseservice.common.utils.CommUtil;
import com.wmh.baseservice.common.utils.page.pojo.PageInfo;
import com.wmh.baseservice.common.utils.page.pojo.PageResult;
import com.wmh.baseservice.common.utils.page.pojo.SortWay;
import com.wmh.baseservice.common.utils.page.util.PageUtil;
import com.wmh.baseservice.common.utils.returnbody.ResultCodeInfo;
import com.wmh.baseservice.common.utils.returnbody.ReturnBody;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import com.wmh.baseservice.admin.service.SysAdviceService;

/**
 * @author mmx generator
 * @since 2021-10-22
 */
@Api(tags = "管理员-用户建议反馈")
@RestController
@RequestMapping(value = "admin/adUsAdvice", produces = "text/plain;charset=utf-8")
@RequiresRoles("admin")
public class AdUsAdviceController {

    @Resource
    private SysAdviceService sysAdviceService;


    @ApiOperation("列表")
    @PostMapping("list")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataTypeClass = Long.class, name = "id", value = "id", required = false),
    })
    public String list(PageInfo pageInfo, Long usId){
        //执行分页辅助工具
        pageInfo.setTimeScreen("create_time");
        pageInfo.setDefaultSort("create_time", SortWay.DESC);
        PageResult<SysAdvice> pageResult = new PageUtil<SysAdvice>(pageInfo).startPage((page, queryWrapper) -> {
            if (CommUtil.checkNull(usId)){
                queryWrapper.eq(SysAdvice.US_ID,usId);
            }
            sysAdviceService.page(page,queryWrapper);
        });
        return ReturnBody.success(pageResult);
    }

    @ApiOperation("回复")
    @PostMapping("reply")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataTypeClass = Long.class, name = "id", value = "id", required = false),
    })
    public String reply(Long id,String content){
        SysAdvice sysAdvice = sysAdviceService.getByIdForUpdate(id);
        if (!CommUtil.checkNull(sysAdvice)||sysAdvice.getStatus()!=0){
            return ReturnBody.error(ResultCodeInfo.SERVICE_EXCEPTION);
        }
        sysAdvice.setStatus(1);
        sysAdvice.setReply(content);
        if (!sysAdviceService.updateById(sysAdvice)) {
            return ReturnBody.error(ResultCodeInfo.SERVICE_EXCEPTION);
        }
        return ReturnBody.success();
    }

}
